package main

import "github.com/lxn/walk"

func showMod(title string, n string, l string) (string, string) {
	var (
		modDia           *walk.Dialog
		gridWidget       *walk.Composite
		label_1          *walk.Label
		nameEdit         *walk.LineEdit
		label_2          *walk.Label
		labelEdit        *walk.LineEdit
		horizontalWidget *walk.Composite
		confirmBtn       *walk.PushButton
		cancelBtn        *walk.PushButton
		name             string
		label            string
	)

	// modDia
	modDia, err = walk.NewDialog(dia)
	ifErr(err)
	err = modDia.SetTitle(title)
	ifErr(err)
	err = modDia.SetMinMaxSize(walk.Size{200, 100}, walk.Size{200, 100})
	ifErr(err)
	modDia.SetFont(font)
	err = modDia.SetLayout(walk.NewVBoxLayout())
	ifErr(err)

	// gridWidget
	gridWidget, err = walk.NewComposite(modDia)
	ifErr(err)
	gridLayout := walk.NewGridLayout()
	err = gridWidget.SetLayout(gridLayout)
	ifErr(err)

	// label_1
	label_1, err = walk.NewLabel(gridWidget)
	ifErr(err)
	err = label_1.SetText("姓名：")
	ifErr(err)
	err = gridLayout.SetRange(label_1, walk.Rectangle{0, 0, 1, 1})
	ifErr(err)

	// nameEdit
	nameEdit, err = walk.NewLineEdit(gridWidget)
	ifErr(err)
	err = gridLayout.SetRange(nameEdit, walk.Rectangle{1, 0, 1, 1})
	ifErr(err)
	err = nameEdit.SetText(n)
	ifErr(err)

	// label_2
	label_2, err = walk.NewLabel(gridWidget)
	ifErr(err)
	err = label_2.SetText("标签：")
	ifErr(err)
	err = gridLayout.SetRange(label_2, walk.Rectangle{0, 1, 1, 1})
	ifErr(err)

	// labelEdit
	labelEdit, err = walk.NewLineEdit(gridWidget)
	ifErr(err)
	err = gridLayout.SetRange(labelEdit, walk.Rectangle{1, 1, 1, 1})
	ifErr(err)
	err = labelEdit.SetText(l)
	ifErr(err)

	// horizontalWidget
	horizontalWidget, err = walk.NewComposite(modDia)
	ifErr(err)
	err = horizontalWidget.SetLayout(walk.NewHBoxLayout())
	ifErr(err)

	// confirmBtn
	confirmBtn, err = walk.NewPushButton(horizontalWidget)
	ifErr(err)
	err = confirmBtn.SetText("确定")
	ifErr(err)

	// cancelBtn
	cancelBtn, err = walk.NewPushButton(horizontalWidget)
	ifErr(err)
	err = cancelBtn.SetText("取消")
	ifErr(err)

	confirmBtn.Clicked().Attach(func() {
		name, label = nameEdit.Text(), labelEdit.Text()
		modDia.Close(0)
	})
	nameEdit.KeyPress().Attach(func(key walk.Key) {
		if key == walk.KeyReturn {
			name, label = nameEdit.Text(), labelEdit.Text()
			modDia.Close(0)
		}
	})
	labelEdit.KeyPress().Attach(func(key walk.Key) {
		if key == walk.KeyReturn {
			name, label = nameEdit.Text(), labelEdit.Text()
			modDia.Close(0)
		}
	})

	// 点击取消时关闭窗口
	cancelBtn.Clicked().Attach(func() {
		modDia.Close(0)
	})

	modDia.Run()
	return name, label
}
